﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Configuration;

namespace HoaDon
{
	public partial class BaoCao : System.Web.UI.Page
	{
		private string ngaybd = "";
		private string ngaykt = "";
		private System.Globalization.CultureInfo cultureinfo = new System.Globalization.CultureInfo("vi-VN");
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				dttuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
				dtdenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");

				drnhanvien.DataSource = Get_NhanVien();
				drnhanvien.DataValueField = "UserID";
				drnhanvien.DataTextField = "UserName";
				drnhanvien.DataBind();
				drnhanvien.SelectedValue = System.Web.HttpContext.Current.User.Identity.Name.ToUpper();

				drcuahang.DataSource = Get_CuaHang();
				drcuahang.DataValueField = "KhoID";
				drcuahang.DataTextField = "KhoName";
				drcuahang.DataBind();
				drcuahang.SelectedValue = "All";

				drkhuvuc.DataSource = Get_KhuVuc();
				drkhuvuc.DataValueField = "NhvvID";
				drkhuvuc.DataTextField = "NhvvName";
				drkhuvuc.DataBind();
				drkhuvuc.SelectedValue = "All";

				drnhomhang.DataSource = Get_NhomHang();
				drnhomhang.DataValueField = "Nhvt2ID";
				drnhomhang.DataTextField = "Nhvt2Name";
				drnhomhang.DataBind();
				drnhomhang.SelectedValue = "All";
			}
			Get_Data();
		}

		protected void btOk_Click(object sender, EventArgs e)
		{
			Get_Data();
		}

		private void Get_Data()
		{
			if (!dttuNgay.Text.ToString().Equals(""))
				ngaybd = DateTime.Parse(dttuNgay.Text, cultureinfo).ToString("yyyyMMdd");
			else
				return;
			if (!dtdenNgay.Text.ToString().Equals(""))
				ngaykt = DateTime.Parse(dtdenNgay.Text, cultureinfo).ToString("yyyyMMdd");
			else
				return;

			string s = "EXEC [fs_HG_BCTHHB2] '" + ngaybd + "','" + ngaykt + "','','','";
			if (drnhanvien.SelectedValue.ToString() != "All")
				s = s + drnhanvien.SelectedValue.ToString() + "','";
			else
				s = s + "" + "','";
			if (drcuahang.SelectedValue.ToString() != "All")
				s = s + drcuahang.SelectedValue.ToString() + "','";
			else
				s = s + "" + "','";
			if (drnhomhang.SelectedValue.ToString() != "All")
				s = s + drnhomhang.SelectedValue.ToString() + "','','','";
			else
				s = s + "" + "','','','";
			if (drkhuvuc.SelectedValue.ToString() != "All")
				s = s + drkhuvuc.SelectedValue.ToString() + "','";
			else
				s = s + "" + "','";

			s = s + Session["FK_DatabaseID"].ToString() + "'";

			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), s);

			if (dt.Rows.Count > 0)
			{
				decimal doanhthu = 0;
				decimal tienvon = 0;
				decimal lai = 0;

				for (int i = 0; i < dt.Rows.Count; i++)
				{
					doanhthu += Convert.ToDecimal(dt.Rows[i]["Doanh_thu"].ToString());
					tienvon += Convert.ToDecimal(dt.Rows[i]["Tien_von"].ToString());
					lai += Convert.ToDecimal(dt.Rows[i]["Lai"].ToString());
				}
				DataRow dr = dt.NewRow();
				dr["Ten_hang"] = "TỔNG CỘNG";
				dr["Doanh_thu"] = doanhthu;
				dr["Tien_von"] = tienvon;
				dr["Lai"] = lai;
				dt.Rows.Add(dr);
			}

			gdxbaocao.DataSource = dt;
			gdxbaocao.DataBind();
		}


		protected void btIn_Click(object sender, EventArgs e)
		{
			string ngaybd_bc = "", ngaykt_bc = "";
			if (!dttuNgay.Text.ToString().Equals(""))
			{
				ngaybd = DateTime.Parse(dttuNgay.Text, cultureinfo).ToString("yyyyMMdd");
				ngaybd_bc = DateTime.Parse(dttuNgay.Text, cultureinfo).ToString("dd/MM/yyyy");
			}
			else
				return;
			if (!dtdenNgay.Text.ToString().Equals(""))
			{
				ngaykt = DateTime.Parse(dtdenNgay.Text, cultureinfo).ToString("yyyyMMdd");
				ngaykt_bc = DateTime.Parse(dtdenNgay.Text, cultureinfo).ToString("dd/MM/yyyy");
			}
			else
				return;

			string s = "EXEC [fs_HG_BCTHHB2] '" + ngaybd + "','" + ngaykt + "','','','";
			if (drnhanvien.SelectedValue.ToString() != "All")
				s = s + drnhanvien.SelectedValue.ToString() + "','";
			else
				s = s + "" + "','";
			if (drcuahang.SelectedValue.ToString() != "All")
				s = s + drcuahang.SelectedValue.ToString() + "','";
			else
				s = s + "" + "','";
			if (drnhomhang.SelectedValue.ToString() != "All")
				s = s + drnhomhang.SelectedValue.ToString() + "','','','";
			else
				s = s + "" + "','','','";
			if (drkhuvuc.SelectedValue.ToString() != "All")
				s = s + drkhuvuc.SelectedValue.ToString() + "','";
			else
				s = s + "" + "','";

			s = s + Session["FK_DatabaseID"].ToString() + "'";

			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), s);
			if (dt.Rows.Count > 0)
			{
				decimal doanhthu = 0;
				decimal tienvon = 0;
				decimal lai = 0;

				for (int i = 0; i < dt.Rows.Count; i++)
				{
					doanhthu += Convert.ToDecimal(dt.Rows[i]["Doanh_thu"].ToString());
					tienvon += Convert.ToDecimal(dt.Rows[i]["Tien_von"].ToString());
					lai += Convert.ToDecimal(dt.Rows[i]["Lai"].ToString());
				}
				DataRow dr = dt.NewRow();
				dr["Ten_hang"] = "TỔNG CỘNG";
				dr["Doanh_thu"] = doanhthu;
				dr["Tien_von"] = tienvon;
				dr["Lai"] = lai;
				dt.Rows.Add(dr);
			}

			rptBCDTBH rpt = new rptBCDTBH { DataSource = dt };
			rpt.Ma_hang.DataBindings.Add("Text", dt, "Ma_hang");
			rpt.Ten_hang.DataBindings.Add("Text", dt, "Ten_hang");
			rpt.Dvt.DataBindings.Add("Text", dt, "Dvt");
			rpt.So_luong.DataBindings.Add("Text", dt, "So_luong", "{0:#,#}");
			rpt.Gia_ban.DataBindings.Add("Text", dt, "Gia_ban", "{0:#,#}");
			rpt.Doanh_thu.DataBindings.Add("Text", dt, "Doanh_thu", "{0:#,#}");
			rpt.Gia_von.DataBindings.Add("Text", dt, "Gia_von", "{0:#,#}");
			rpt.Tien_von.DataBindings.Add("Text", dt, "Tien_von", "{0:#,#}");
			rpt.Lai.DataBindings.Add("Text", dt, "Lai", "{0:#,#}");
			rpt.Parameters["tungay"].Value = ngaybd_bc;
			rpt.Parameters["denngay"].Value = ngaykt_bc;
			Session["Report"] = rpt;
			Response.Redirect("~\\Print.aspx");
		}

		private DataTable Get_NhanVien()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(WebConfigurationManager.ConnectionStrings["proxyconn"].ConnectionString, "SELECT UserID, UserName FROM Userinfo WHERE FK_DatabaseID = (SELECT FK_DatabaseID FROM Userinfo WHERE UserID = '" + System.Web.HttpContext.Current.User.Identity.Name.ToUpper() + "')");
			DataRow newR = dt.NewRow();
			newR["UserID"] = "All";
			newR["UserName"] = "Tất cả";
			dt.Rows.Add(newR);
			return dt;
		}

		private DataTable Get_CuaHang()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT * FROM dmkho WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND Status = '1'");

			DataRow newR = dt.NewRow();
			newR["KhoID"] = "All";
			newR["KhoName"] = "Tất cả";
			dt.Rows.Add(newR);
			return dt;
		}

		private DataTable Get_KhuVuc()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT NhvvID, NhvvName FROM dmnhvv WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND Status = '1'");
			DataRow newR = dt.NewRow();
			newR["NhvvID"] = "All";
			newR["NhvvName"] = "Tất cả";
			dt.Rows.Add(newR);
			return dt;
		}

		private DataTable Get_NhomHang()
		{
			DataTable dt = new DataTable();
			dt = SqlEXEC.GetData(Session["strConn"].ToString(), "SELECT Nhvt2ID, Nhvt2Name FROM dmnhvt2 WHERE FK_DatabaseID = '" + Session["FK_DatabaseID"] + "' AND NL_yn = 0 AND TP_yn = 1 AND Status = '1'");
			DataRow newR = dt.NewRow();
			newR["Nhvt2ID"] = "All";
			newR["Nhvt2Name"] = "Tất cả";
			dt.Rows.Add(newR);
			return dt;
		}
	}
}
